package com.wx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.PageParam;
import com.wx.common.SecurityUser;
import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.domain.dto.DayReportQueryParam;
import com.wx.domain.dto.SysPatientDto;
import com.wx.domain.vo.NewJoinGroupStatistics;
import com.wx.domain.vo.PatientVo;
import com.wx.entity.SysAeReportLog;
import com.wx.entity.SysPatient;
import com.wx.holder.SecurityUserHolder;
import com.wx.service.SysAeReportLogService;
import com.wx.service.SysPatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;



/**
 * <p>Title:      SysPatientController. </p>
 * <p>Description  </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         yangwensheng@100doc.com.cn
 * @since      2021-03-03 11:36:07
 */
@RestController
@RequestMapping(value = "/sysPatient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "患者管理和AE管理", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysPatientController {

    @Resource
    private SysPatientService sysPatientService;

    @Resource
    private SysAeReportLogService sysAeReportLogService;

    @Resource
    private SecurityUserHolder securityUserHolder;


    @PostMapping("/listPage")
    @ApiOperation(httpMethod = "POST", value = "患者分页列表查询")
    public Wrapper<PageInfo<PatientVo>> listPage(@ApiParam(name = "sysPatient", value = "sysPatient") @RequestBody PageParam<SysPatientDto> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<PatientVo> list = sysPatientService.listPage(pageParam.getParam());
        PageInfo<PatientVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "删除患者")
    public Wrapper<Integer> deletePatient(@PathVariable("id") String id) {
        SecurityUser user = securityUserHolder.getUser();
        int result = sysPatientService.deletePatient(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "更新患者信息")
    public Wrapper<?> update(@ApiParam(name = "sysPatient", value = "sysPatient") @RequestBody SysPatient sysPatient) {
        SecurityUser user = securityUserHolder.getUser();
        sysPatient.setUpdater(user.getRealName());
        sysPatient.setUpdateBy(user.getUserId());
        sysPatient.setUpdateDate(new Date());
        int result = sysPatientService.update(sysPatient);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @PostMapping(value = "/export")
    @ApiOperation(httpMethod = "POST", value = "导出患者")
    public void export(HttpServletResponse response, @ApiParam(name = "sysPatient", value = "sysPatient") @RequestBody SysPatientDto sysPatient) throws IOException {
        List<PatientVo> list = sysPatientService.listPage(sysPatient);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("患者列表.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), PatientVo.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("患者列表").doWrite(list);
    }


    @GetMapping("/listReportLog/{patientId}")
    @ApiOperation(httpMethod = "GET", value = "根据患者id查询AE上报记录")
    public Wrapper<List<SysAeReportLog>> listReportLog(@PathVariable("patientId") String patientId) {
        List<SysAeReportLog> list = sysAeReportLogService.listReportLog(patientId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }


    @GetMapping("/queryReportLog/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据上报id查询单条AE上报记录")
    public Wrapper<SysAeReportLog> queryReportLogByPatientId(@PathVariable("id") String id) {
        SysAeReportLog sysAeReportLog = sysAeReportLogService.selectByKey(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, sysAeReportLog);
    }


    @PostMapping(value = "/export/newJoinGroup")
    @ApiOperation(httpMethod = "POST", value = "新入组统计")
    public void exportNewJoinGroup(HttpServletResponse response, @RequestBody DayReportQueryParam dayReportQeuryParam) throws IOException {
        List<NewJoinGroupStatistics> newJoinGroupStatistics = sysPatientService.exportNewJoinGroup(dayReportQeuryParam);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("新入组统计.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), NewJoinGroupStatistics.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("处方统计").doWrite(newJoinGroupStatistics);
    }

}